package test.time;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

public class TimeUtils {

    private static final SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat();

    /**
     * @param firstOrLast true : 本月第1天,false : 本月最后一天
     * @param pattern  PatternType
     * @return
     */
    public static String currentMonthDate(boolean firstOrLast, @PatternType String pattern) {
        Calendar calendar = Calendar.getInstance();
        if (firstOrLast) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
        } else {
            calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        }
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取本周指定某天的日期.
     *
     * @param weekDayType 1: 周日,2: 周一,3: 周二,4: 周三,5: 周四,6: 周五,7: 周六,
     * @return 本周指定某天的日期.
     */
    public static String currentWeekDate(int weekDayType, @PatternType String pattern) {
        Calendar calendar = Calendar.getInstance();
        int currentWeekDayType = calendar.get(Calendar.DAY_OF_WEEK);
        //4-5 = 1
        int differCount = weekDayType - currentWeekDayType;
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DATE) + differCount);
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(calendar.getTime());
    }

    public static String stringForTime(long timeMs) {
        long totalSeconds = timeMs / 1000;

        long seconds = totalSeconds % 60;
        long minutes = (totalSeconds / 60) % 60;
        long hours = totalSeconds / 3600;
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    /**
     * 将给定的时间设置时,分
     *
     * @param hour    小时
     * @param minute  分钟
     * @param pattern 时间格式
     * @return 指定格式的时间.
     */
    public static String changeTime(String time, @PatternType String pattern, int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        long stamp = timeToStamp(time, pattern);
        calendar.setTimeInMillis(stamp);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取当前时间.
     *
     * @param pattern 格式
     * @return 当前时间
     */
    public static String currentTime(@PatternType String pattern) {
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 使用Calendar获取指定时间格式.
     *
     * @param year    年份
     * @param month   月份
     * @param date    日期
     * @param pattern 时间格式
     * @return 指定格式的时间.
     */
    public static String getTimeByCalendar(int year, int month, int date, @PatternType String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, date);
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(calendar.getTime());
    }

    /**
     * 判断是否为周末.
     *
     * @param stamp 日期时间戳
     * @return true为周末
     */
    public static boolean isWeekend(long stamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(stamp));
        return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }

    /***
     * 根据当前时间推算dayCount天前或dayCount天后的时间.
     * @param dayCount  相差的天数.
     * @return 推算dayCount天前或dayCount天后的时间.
     */
    public static String calculateTime(int dayCount, @PatternType String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + dayCount);
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(calendar.getTime());
    }

    /***
     * 根据指定时间推算dayCount天前或dayCount天后的时间.
     * @param dayCount  相差的天数.
     * @return 推算dayCount天前或dayCount天后的时间.
     */
    public static String calculateTime(int dayCount, String date, @PatternType String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timeToStamp(date, pattern)));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + dayCount);
        mSimpleDateFormat.applyPattern(pattern);
        return mSimpleDateFormat.format(calendar.getTime());
    }

    /**
     * 将时间字符串转换为时间戳.
     *
     * @param time        时间字符串
     * @param patternType 格式化类型:
     * @return 时间戳
     */
    public static long timeToStamp(String time, @PatternType String patternType) {
        mSimpleDateFormat.applyPattern(patternType);
        Date date = null;
        try {
            date = mSimpleDateFormat.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
            return 0l;
        }
        return date.getTime();
    }

    /**
     * 判断给定日期是否和昨天是同一天.
     *
     * @param dateText dateText 给定日期字符串.
     * @param pattern  时间格式.
     * @return true:为同一天.yesterday
     */
    public static boolean isYesterday(String dateText, @PatternType String pattern) {
        mSimpleDateFormat.applyPattern(pattern);
        Calendar dateCalendar = Calendar.getInstance();
        try {
            dateCalendar.setTime(mSimpleDateFormat.parse(dateText));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        int dateYear = dateCalendar.get(Calendar.YEAR);
        int dateMonth = dateCalendar.get(Calendar.MONTH);
        int dateDay = dateCalendar.get(Calendar.DAY_OF_MONTH);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        int yesterdayYear = calendar.get(Calendar.YEAR);
        int yesterdayMonth = calendar.get(Calendar.MONTH);
        int yesterdayDay = calendar.get(Calendar.DAY_OF_MONTH);
        return dateYear == yesterdayYear && dateMonth == yesterdayMonth && dateDay == yesterdayDay;
    }
}
